﻿using Protocol.Code;
using Protocol.Dto;
using Script.Framework;
using Script.Model;
using Script.UI;
using UnityEngine;

namespace Script.Net.Impl
{
    public class UserHandler : HandlerBase
    {
        public override void OnReceive(int subCode, object value)
        {
            switch (subCode)
            {
                case UserCode.CREAT_SRES:
                    CreateResponse((int) value);
                    break;
                case UserCode.Get_INFO_SRES:
                    getInfoResponse(value as UserDto);
                    break;
                case UserCode.ONLINE_SRES:
                    onlineResponse((int) value);
                    break;
                default:
                    Debug.Log("Something wrong");
                    break;
            }
        }

        private readonly SocketMsg socketMsg = new SocketMsg();

        /// <summary>
        /// 获取信息的回应
        /// </summary>
        private void getInfoResponse(UserDto user)
        {
            if (user == null)
            {
                // 没有角色的回应
                // 显示创建面板
                Dispatch(AreaCode.UI, UIEvent.CREATE_PANEL_ACTIVE, true);
            }
            else
            {
                // 有角色
                // 隐藏创建面板
                Dispatch(AreaCode.UI, UIEvent.CREATE_PANEL_ACTIVE, false);

                // 角色上线
                socketMsg.Change(OpCode.USER, UserCode.ONLINE_CREQ, null);
                Dispatch(AreaCode.NET, 0, socketMsg);


                // 保存服务发送来的用户信息
                GameModel model = Models.GameModel;

                Debug.Log("我的数据来了吗");
                model.UserDto = user;

                // 更新本地的显示
                Dispatch(AreaCode.UI, UIEvent.REFRESH_INFO_PANEL, user);
            }
        }

        /// <summary>
        /// 上线的响应
        /// </summary>
        private void onlineResponse(int result)
        {
            switch (result)
            {
                case 0:
                    // 上线成功
                    Debug.Log("上线成功");
                    break;
                case 1:
                    Debug.LogError("客户端非法登录");
                    break;
                case 2:
                    Debug.LogError("非法操作，角色已经上线");
                    break;
            }
        }

        /// <summary>
        /// 创建角色的响应
        /// </summary>
        /// <param name="result"></param>
        private void CreateResponse(int result)
        {
            switch (result)
            {
                case -1:
                    Debug.LogError("客户端非法登录");

                    break;
                case -2:
                    Debug.LogError("非法操作，角色已经上线");
                    break;
                case 0:
                    Debug.Log("创建成功");
                    // 创建成功
                    // 隐藏创建面板
                    Dispatch(AreaCode.UI, UIEvent.CREATE_PANEL_ACTIVE, false);
                    // 获取角色信息
                    socketMsg.Change(OpCode.USER, UserCode.Get_INFO_CREQ, null);
                    Dispatch(AreaCode.NET, 0, socketMsg);
                    break;
            }
        }
    }
}